package com.youli.gb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;

import com.youli.gb.constant.SysConstatnt;
import com.youli.gb.model.Message;
import com.youli.gb.model.SysUser;
import com.youli.gb.service.IMessageService;
import com.youli.gb.service.impl.MessageServiceImpl;

@WebServlet("/message.do")
public class MessageServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;	
	
	private IMessageService messageService = new MessageServiceImpl();	
       
    public MessageServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session=request.getSession();
		SysUser loginUser=(SysUser) session.getAttribute(SysConstatnt.SESSION_USER);
		String type = request.getParameter("type");
		
		if (type.equals("list")) {
			list(request, response, loginUser);
		}
		else if (type.equals("add"))
		{
			add(request, response);
		}
	    else if (type.equals("edit")) 
	    {
			edit(request, response);
	    }
	    else if (type.equals("zan")) 
	    {
	    	zan(request, response);
	    }
	    else if (type.equals("delete")) 
	    {
	    	delete(request, response);
		}
	    else if (type.equals("stop")) 
	    {
	    	int id=Integer.valueOf(request.getParameter("id"));
			int ret=this.messageService.stop(id);
			if (ret > 0) {
				request.setAttribute("messageResult", "禁用成功！");
			}
			else{
					request.setAttribute("messageResult", "禁用失败！");
			}
			request.getRequestDispatcher("/message.do?type=list").forward(request, response);
	    }
		
		
	   }

	private void list(HttpServletRequest request, HttpServletResponse response, SysUser loginUser)
			throws ServletException, IOException {
		List<Message> messages=null;
		if(loginUser !=null && loginUser.getRole().getId()==1) {
			messages = this.messageService.getAll2();
		}
		else {
			messages = this.messageService.getAll();
		}
		request.setAttribute("message", messages);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.valueOf(request.getParameter("id"));
		int ret=this.messageService.delete(id);
		if (ret > 0) {
			request.setAttribute("messageResult", "删除成功！");
		}
		else{
				request.setAttribute("messageResult", "删除失败！");
   }
   request.getRequestDispatcher("/message.do?type=list").forward(request, response);
	}

	private void zan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.valueOf(request.getParameter("id"));
		int ret=this.messageService.zan(id);
		if (ret > 0) {
			request.setAttribute("messageResult", "点赞成功！");
   }else {
			request.setAttribute("messageResult", "点赞失败！");
   }
   request.getRequestDispatcher("/message.do?type=list").forward(request, response);
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id=Integer.valueOf(request.getParameter("id"));
String title=request.getParameter("title");
String content=request.getParameter("content");
//		保存
Message message=new Message(id, title, content);
int ret=this.messageService.edit(message);
if (ret > 0) {
		request.setAttribute("messageResult", "留言修改成功！");
}else {
		request.setAttribute("messageResult", "留言修改失败！");
}
request.getRequestDispatcher("/message.do?type=list").forward(request, response);
	}
	
		

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取参数
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		//获取已登录用户
		HttpSession session = request.getSession();
		SysUser loginUser =  (SysUser) session.getAttribute(SysConstatnt.SESSION_USER);
		//添加操作
		if (StringUtils.isNoneBlank(title) && StringUtils.isNoneBlank(content)) {
			Message message = new Message(title, content, loginUser);
			int ret = this.messageService.add(message);
			if (ret > 0) {
				request.setAttribute("messageResult", "留言成功！");
			}else {
				request.setAttribute("messageResult", "留言失败！");
			}
		}
		request.getRequestDispatcher("/message.do?type=list").forward(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}




















